﻿package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import flash.net.URLVariables;
	
	
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.events.MeetingEvent;
	import Welsy4.Connect.business.MeetingDelegate;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.SCOShortcut;
	import Welsy4.Connect.vo.Meeting;
	
	
	public class UpdateMeetingCommand extends SequenceCommand implements Command, IResponder
	{
		
		override public function execute(event:CairngormEvent):void {
			 
			
			ConnectModel.getInstance().addQueue("UpdateMeetingCommand.execute : " + ConnectModel.getInstance().lastMeetingFound.name + " ("+ ConnectModel.getInstance().lastMeetingFound.sco_id+ ")");
			
			
			ConnectModel.getInstance().lastMeetingFound.description = MeetingEvent(event).meeting.description;
			ConnectModel.getInstance().lastMeetingFound.date_begin = MeetingEvent(event).meeting.date_begin;
			ConnectModel.getInstance().lastMeetingFound.date_end = MeetingEvent(event).meeting.date_end;
			
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			
			// UpdateMeetingCommand
			var meetingDelegate:MeetingDelegate = new MeetingDelegate(this);
			
			
			meetingDelegate.updateMeeting(ConnectModel.getInstance().lastMeetingFound);
			
			
		}
		
		public function result(data:Object):void {
			
			ConnectModel.getInstance().addQueue("UpdateMeetingCommand.result");
		
			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(data.result);
			
			if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
				var meeting:Meeting = new Meeting;
				Alert.show("Everything works ok!\n" + data.result);
				/*if (ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.length() > 0) {
					
					meeting.sco_id = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.attribute("sco-id");
					meeting.name = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.name;
					
					ConnectModel.getInstance().lastMeetingFound = meeting;
					
					//Alert.show("meeting found: " + meeting.sco_id );
					
					this.executeNextCommand();
					
				} else {
					CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.NO_DATA, data.result));
				}
				*/
				//this.executeNextCommand();
			} else {
				Alert.show("Everything no works!\n" + data.result);
			    CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.GENERIC, data.result));	
			}
			
			
			
			
		}
		
		public function fault(data:Object):void {
		    ConnectModel.getInstance().addQueue("UpdateMeetingCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}
		
	}
	
}